﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PlanningGIS.Framework;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Win.Controls;
////using ESRI.ArcGIS.TOCControl;


namespace PlanningGIS.TocContextMenu.Button
{

    /// <summary>
    /// 作用：TOC右键菜单保存图层文件按钮类
    /// 作者：
    /// 编写日期:2008-08-01
    internal  class SaveLayerFile:BaseCommand
    {
        IApplication m_Application = null;
        public SaveLayerFile()
        {
            base.m_caption = "保存为图层文件";
            base.m_toolTip = base.Caption;
        }

        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        public override void OnClick()
        {
      
            try
            {
     
                ILayer pLayer = (m_Application as IFramework).TOCControl.CustomProperty as ILayer;

                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "图层文件(*.lyr)|*.lyr";
                saveFileDialog.Title = "保存为图层文件";
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.FileName =
                   System.IO.Path.Combine(saveFileDialog.InitialDirectory,
                                 pLayer.Name);

                //获得图层名
                DialogResult dr = saveFileDialog.ShowDialog();
                if (saveFileDialog.FileName != "" && dr == DialogResult.OK)
                {
                    if (File.Exists(saveFileDialog.FileName))
                    {
                        //删除已经存在的文件
                        File.Delete(saveFileDialog.FileName);
                    }
                    ILayerFile layerFile = new LayerFileClass(); //创建图层文件实例
                    layerFile.New(saveFileDialog.FileName); //创建图层文件
                    layerFile.ReplaceContents(pLayer); //和实际图层相关联
                    layerFile.Save(); //保存图层文件
                }
            }
            catch (System.Exception)
            {
                MsgHelper.ShowErrorMsgbox("保存为图层文件出错");
            }

        }

        public override bool Enabled
        {
            get
            {
                return ((m_Application as IFramework).IsMapCtrlactive && (m_Application as IFramework).TOCControl.CustomProperty is ILayer) ? true : false;
            }
        }
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }

    }
    
}
